#pragma once
#include <typescommon.h>
#include "rbtree_augmented.h"
typedef int (*rbCompareFunctionType)(const void *keya, int asize, const void *keyb, int bsize);
typedef int (*rbTraversalFunctionType)(const void *key, int keysize, const void *value, int valsize);
struct rbtreeManagerStruct
{
    struct rb_root rbroot;
    rbCompareFunctionType cmpfunc;
    pthread_mutex_t rbtreelock;
    uint64_t treesize;
    rbTraversalFunctionType traversalfunc; // 遍历时回调函数。
};

struct rbtreeNodeCommonInform
{
    struct rb_node node;
    uint32_t keysize;
    uint32_t valsize;
    void *rbkey;
    void *rbval;
};
#define rbConvertUserNode(rbnode) ((struct rbtreeNodeCommonInform *)rbnode)

int rbtreeRootInit(struct rbtreeManagerStruct *rbmng, rbCompareFunctionType setcmp, rbTraversalFunctionType settraversal);
int rbtreeRootFree(struct rbtreeManagerStruct *rbmng);
int rbtreeInsertOperate(struct rbtreeManagerStruct *rbmng, const void *inkey, int keysize, void *indata, uint32_t datasize);
// 替换数据后，将旧的用户数据返回，让其释放。
int rbtreeReplaceOperate(struct rbtreeManagerStruct *rbmng, const void *inkey, int keysize, void *invalue, uint32_t valuesize, void **oldval);
// 擦除需要返回用户数据，让其释放。
int rbtreeFindWithErase(struct rbtreeManagerStruct *rbmng, const void *inkey, int keysize, void **outvalue, uint32_t *outsize, int eraseflag);
// 擦除需要返回用户数据，让其释放。
int rbtreeGetRootWithErase(struct rbtreeManagerStruct *rbmng, void **outvalue, uint32_t *outsize, int eraseflag);
// 擦除需要返回用户数据，让其释放。
int rbtreeGetLeftesWithErase(struct rbtreeManagerStruct *rbmng, void **outvalue, uint32_t *outsize, int eraseflag);
// 擦除需要返回用户数据，让其释放。
int rbtreeGetRightesWithErase(struct rbtreeManagerStruct *rbmng, void **outvalue, uint32_t *outsize, int eraseflag);
// traversalflag,0:前序，1：中序，2：后序
void rbtreeTraversalRoot(struct rbtreeManagerStruct *rbmng, int traversalflag);
